<template>
  <div class="order-progress">
    <div class="progress-header">
      <div class="status-info">
        <div class="status-title">{{ statusInfo.title }}</div>
        <div class="status-desc">{{ statusInfo.description }}</div>
      </div>
      <!-- <div v-if="statusInfo.estimatedTime" class="estimated-time">
        预计{{ statusInfo.estimatedTime }}
      </div> -->
    </div>
    
    <div class="progress-steps">
      <div 
        v-for="(step, index) in progressSteps" 
        :key="index"
        :class="[
          'progress-step',
          { 
            'completed': step.completed,
            'active': step.active,
            'current': step.current
          }
        ]"
      >
        <div class="step-icon">
          <el-icon v-if="step.completed" color="#67C23A"><Check /></el-icon>
          <el-icon v-else-if="step.active || step.current" :color="step.current ? '#409EFF' : '#909399'">
            <component :is="step.icon" />
          </el-icon>
          <el-icon v-else color="#DCDFE6"><component :is="step.icon" /></el-icon>
        </div>
        <div class="step-content">
          <div class="step-title">{{ step.title }}</div>
          <div v-if="step.time" class="step-time">{{ formatTime(step.time) }}</div>
        </div>
        <div v-if="index < progressSteps.length - 1" class="step-line" :class="{ 'completed': step.completed }"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { Check, Money, Shop, Bowl, Van, Star } from '@element-plus/icons-vue';
import { OrderStatus } from '@/types/customer';

// Props
interface Props {
  status: string;
  orderTime?: string;
  payTime?: string;
  acceptTime?: string;
  prepareTime?: string;
  pickupTime?: string;
  deliverTime?: string;
  completeTime?: string;
}

const props = withDefaults(defineProps<Props>(), {
  status: OrderStatus.ORDER_CREATED
});

// 状态信息映射
const statusInfo = computed(() => {
  const normalizedStatus = props.status;
  
  const statusMap = {
    'ORDER_CREATED': {
      title: '等待支付',
      description: '请尽快完成支付，订单将为您保留15分钟',
      estimatedTime: null
    },
    'PENDING': {
      title: '等待商家接单',
      description: '您的订单已支付成功，等待商家接单',
      estimatedTime: '5分钟内接单'
    },
    'MERCHANT_CONFIRMED': {
      title: '商家已接单',
      description: '商家已确认您的订单，正在准备制作',
      estimatedTime: '20分钟后出餐'
    },
    'READY_WAITING_RIDER': {
      title: '等待骑手取餐',
      description: '美食已制作完成，等待骑手取餐',
      estimatedTime: '10分钟内取餐'
    },
    'RIDER_ASSIGNED': {
      title: '骑手已接单',
      description: '骑手已接单，准备取餐配送',
      estimatedTime: '20分钟内送达'
    },
    'FOOD_PICKED': {
      title: '骑手已取餐',
      description: '骑手已取餐，正在为您配送',
      estimatedTime: '15分钟内送达'
    },
    'DELIVERED': {
      title: '订单已送达',
      description: '订单已送达，请确认收货',
      estimatedTime: null
    },
    'COMPLETED': {
      title: '订单已完成',
      description: '感谢您的订餐，期待下次为您服务',
      estimatedTime: null
    },
    'CANCELLED': {
      title: '订单已取消',
      description: '订单已取消，如有疑问请联系客服',
      estimatedTime: null
    }
  };
  
  return statusMap[normalizedStatus] || statusMap['ORDER_CREATED'];
});

// 进度步骤
const progressSteps = computed(() => {
  const currentStatus = props.status;
  
  const steps = [
    {
      key: 'PENDING',
      title: '订单已支付',
      icon: Money,
      time: props.payTime,
      completed: false,
      active: false,
      current: false
    },
    {
      key: 'MERCHANT_CONFIRMED',
      title: '商家已接单',
      icon: Shop,
      time: props.acceptTime,
      completed: false,
      active: false,
      current: false
    },
    {
      key: 'READY_WAITING_RIDER',
      title: '等待骑手取餐',
      icon: Bowl,
      time: props.prepareTime,
      completed: false,
      active: false,
      current: false
    },
    {
      key: 'RIDER_ASSIGNED',
      title: '骑手已接单',
      icon: Van,
      time: props.pickupTime,
      completed: false,
      active: false,
      current: false
    },
    {
      key: 'FOOD_PICKED',
      title: '骑手已取餐',
      icon: Van,
      time: props.pickupTime,
      completed: false,
      active: false,
      current: false
    },
    {
      key: 'DELIVERED',
      title: '订单已送达',
      icon: Star,
      time: props.deliverTime,
      completed: false,
      active: false,
      current: false
    }
  ];
  
  // 根据当前状态设置步骤状态
  const statusOrder = [
    'ORDER_CREATED',           // 0 - 待支付（不在进度条中显示）
    'PENDING',                 // 1 - 已支付，等待商家接单
    'MERCHANT_CONFIRMED',      // 2 - 商家已接单
    'READY_WAITING_RIDER',     // 3 - 等待骑手取餐
    'RIDER_ASSIGNED',          // 4 - 骑手已接单
    'FOOD_PICKED',             // 5 - 骑手已取餐
    'DELIVERED',               // 6 - 订单已送达
    'COMPLETED'                // 7 - 订单已完成
  ];
  
  const currentIndex = statusOrder.indexOf(currentStatus);
  
  steps.forEach((step, index) => {
    const stepIndex = statusOrder.indexOf(step.key);
    if (stepIndex <= currentIndex && stepIndex >= 0) {
      if (stepIndex < currentIndex) {
        step.completed = true;
      } else if (stepIndex === currentIndex) {
        step.current = true;
        step.active = true;
      }
    } else if (stepIndex === currentIndex + 1) {
      step.active = true;
    }
  });
  
  return steps;
});

// 格式化时间
const formatTime = (time?: string): string => {
  if (!time) return '';
  const date = new Date(time);
  return date.toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
};
</script>

<style scoped>
.order-progress {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.progress-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.status-info {
  flex: 1;
}

.status-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 6px;
}

.status-desc {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}

.estimated-time {
  font-size: 12px;
  color: #409EFF;
  background: #f0f8ff;
  padding: 4px 8px;
  border-radius: 4px;
  white-space: nowrap;
}

.progress-steps {
  position: relative;
}

.progress-step {
  display: flex;
  align-items: flex-start;
  position: relative;
  padding-bottom: 25px;
}

.progress-step:last-child {
  padding-bottom: 0;
}

.step-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border: 2px solid #DCDFE6;
  margin-right: 15px;
  flex-shrink: 0;
  position: relative;
  z-index: 2;
}

.progress-step.completed .step-icon {
  background: #67C23A;
  border-color: #67C23A;
  color: white;
}

.progress-step.current .step-icon {
  background: #409EFF;
  border-color: #409EFF;
  color: white;
}

.progress-step.active .step-icon {
  border-color: #409EFF;
  background: white;
}

.step-content {
  flex: 1;
  padding-top: 4px;
}

.step-title {
  font-size: 14px;
  color: #333;
  font-weight: 500;
  margin-bottom: 4px;
}

.progress-step.completed .step-title {
  color: #67C23A;
}

.progress-step.current .step-title {
  color: #409EFF;
  font-weight: 600;
}

.step-time {
  font-size: 12px;
  color: #999;
}

.step-line {
  position: absolute;
  left: 15px;
  top: 32px;
  bottom: -25px;
  width: 2px;
  background: #DCDFE6;
  z-index: 1;
}

.step-line.completed {
  background: #67C23A;
}

.progress-step:last-child .step-line {
  display: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .order-progress {
    padding: 15px;
  }
  
  .progress-header {
    flex-direction: column;
    gap: 10px;
  }
  
  .status-title {
    font-size: 16px;
  }
  
  .step-icon {
    width: 28px;
    height: 28px;
    margin-right: 12px;
  }
  
  .step-line {
    left: 13px;
    top: 28px;
  }
}
</style>
